<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="身份" prop="status">
        <el-select
          v-model="queryParams.agentLevel"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in agentLevelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所在地区" prop="provinceId">
        <el-cascader
          v-model="selectAreaInfo"
          style="width: 240px"
          placeholder="请选择"
          :props="{label:'areaName',value:'id',checkStrictly: true}"
          :options="areaInfo"
          v-on:change="onChangeArea"
          filterable></el-cascader>
      </el-form-item>
      <el-form-item label="邀请码" prop="inviteCode">
        <el-input
          style="width: 240px"
          v-model="queryParams.inviteCode"
          placeholder="请输入邀请码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系姓名" prop="agentName">
        <el-input
          style="width: 240px"
          v-model="queryParams.agentName"
          placeholder="请输入联系姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          style="width: 240px"
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['agent:agent_user:add']">
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['agent:agent_user:remove']">
          删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="身份" align="center" width="80" prop="agentLevel">
        <template slot-scope="scope">
          <p>{{selectDictLabel(agentLevelOptions,scope.row.agentLevel)}}</p>
        </template>
      </el-table-column>
      <el-table-column label="登录手机" align="center" width="130" prop="username"/>
      <el-table-column label="联系姓名" align="center" width="130" prop="agentName"/>
      <el-table-column label="电话号码" align="center" width="130" prop="phone"/>
      <el-table-column label="邀请码" align="center" width="120" prop="inviteCode"/>
      <el-table-column label="所在地区" align="center" min-width="200" prop="inviteCode">
        <template slot-scope="scope">
          <p>
            {{(scope.row.provinceName == null ? "" : scope.row.provinceName )+" "+
            (scope.row.cityName == null ? "" : scope.row.cityName) + " "+
            (scope.row.districtName == null ? "" : scope.row.districtName)}}
          </p>
        </template>
      </el-table-column>
      <el-table-column label="直推人数" align="center" width="100" prop="directTotali">
        <template slot-scope="scope">
          <p>{{scope.row.directTotali}}人</p>
        </template>
      </el-table-column>
      <el-table-column label="间荐人数" align="center" width="100" prop="indirectTotali">
        <template slot-scope="scope">
          <p>{{scope.row.indirectTotali}}人</p>
        </template>
      </el-table-column>
      <el-table-column label="直推比例" align="center" width="100" prop="directScale">
        <template slot-scope="scope">
          <p>{{scope.row.directScale}}%</p>
        </template>
      </el-table-column>
      <el-table-column label="间推比例" align="center" width="100" prop="indirectScale">
        <template slot-scope="scope">
          <p>{{scope.row.indirectScale}}%</p>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="100" prop="status">
        <template slot-scope="scope">
          <p>{{selectDictLabel(statusOptions,scope.row.status)}}</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-s-data" @click="$router.push('/agent/statistics/'+scope.row.id)">
            统计
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['agent:agent_user:edit']">
            修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['agent:agent_user:remove']">
            删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-key"
            @click="handleResetPwd(scope.row)"
            v-hasPermi="['agent:agent_user:edit']"
          >重置
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"/>

    <add-or-update v-show="open" @on-close="open=false" ref="addOrUpdate"></add-or-update>
  </div>
</template>

<script>
  import {
    resetAgentPwd,
    listAgent_user,
    getAgent_user,
    delAgent_user,
    exportAgent_user
  } from "@/api/agent/agent_user";
  import {getAreaList} from "@/api/common";
  import mixinViewModule from '@/mixins/view-module'
  import AddOrUpdate from './add-or-update'

  export default {
    name: "Agent_user",
    mixins: [mixinViewModule],
    components: {AddOrUpdate},
    data() {
      return {
        mixinViewModuleOptions: {
          getDetailsURL: getAgent_user,// 查询详情实体
          getDataListURL: listAgent_user, // 数据列表接口，API地址
          deleteURL: delAgent_user, // 删除接口，API地址
          deleteIsBatchKey: 'id', // 删除接口，批量状态下由那个key进行标记操作？比如：pid，uid...
          exportURL: exportAgent_user // 导出接口，API地址
        },
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        statusOptions: [],
        agentLevelOptions: [],
        areaInfo: [],
        selectAreaInfo: [],
        // 查询参数
        queryParams: {
          isAsc: "desc",
          orderByColumn: 't.create_time',
          status: null,
          agentLevel: null,
          cityId: null,
          agentName: null,
          phone: null,
          districtId: null,
          username: null,
          password: null,
          provinceId: null,
          inviteCode: null,
          directScale: null,
          chain: null,
          indirectScale: null,
          pid: null,
          address: null,
        }
      }
        ;
    },
    created() {
      this.getList();
      let _this = this;
      this.getDicts('sys_agent_level').then(response => {
        _this.agentLevelOptions = response.data
        _this.$refs.addOrUpdate.agentLevelOptions = response.data
      })
      this.getDicts("sys_normal_disable").then(response => {
        _this.statusOptions = response.data;
        _this.$refs.addOrUpdate.statusOptions = response.data
      });
      getAreaList().then(response => {
        _this.areaInfo = response.data;
        _this.$refs.addOrUpdate.areaInfo = response.data
      })
    },
    methods: {
      //打开dislog前执行
      onOpenHandleDialog() {

      },
      onChangeArea(value) {
        if (value.length > 0) this.queryParams.provinceId = value[0];
        if (value.length > 1) this.queryParams.cityId = value[1];
        if (value.length > 2) this.queryParams.districtId = value[2];

      },
      //打开删除前执行
      onHandleDetails(data, type) {
        if (type == this.$formType.edit) {
          data.data.areaInfo = [];
          if (data.data.provinceId != null) data.data.areaInfo.push(data.data.provinceId);
          if (data.data.cityId != null) data.data.areaInfo.push(data.data.cityId);
          if (data.data.districtId != null) data.data.areaInfo.push(data.data.districtId);
          this.$refs.addOrUpdate.form = data.data
        }
      },
      //开始查询列表的时候
      onStartQuery() {
      },
      /** 重置密码按钮操作 */
      handleResetPwd(row) {
        this.$prompt('请输入"' + row.agentName + '"的新密码', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({value}) => {
          resetAgentPwd(row.id, value).then(response => {
            if (response.code === 200) {
              this.msgSuccess('修改成功，新密码是：' + value)
            } else {
              this.msgError(response.msg)
            }
          })
        }).catch(() => {
        })
      },
      //开始重制查询前执行
      onResetQuery() {
        this.selectAreaInfo = [];
        this.queryParams = {
          status: null,
          agentLevel: null,
          cityId: null,
          agentName: null,
          phone: null,
          districtId: null,
          username: null,
          password: null,
          provinceId: null,
          inviteCode: null,
          directScale: null,
          chain: null,
          indirectScale: null,
          pid: null,
          address: null,
        }
      }
    }
  };
</script>
